﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataLib.Accounts.Bus;

namespace DataLib.Accounts
{
    public partial class UserAdd : System.Web.UI.Page
    {
        protected System.Web.UI.HtmlControls.HtmlInputButton btnCancel;
        public string adminname = "管理部门";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindDropDownList();
            }
        }

        public void BindDropDownList()
        {
  
            DataLib.BLL.Sys_Library bllLib = new DataLib.BLL.Sys_Library();
            //权限级别 下拉列表
            dropPermiessLev.DataSource = bllLib.GetLibByCate("权限分级");
            dropPermiessLev.DataTextField = "libValue";
            dropPermiessLev.DataValueField = "libKey";
            dropPermiessLev.DataBind();

            //部门下拉列表
            dropDepart.DataSource = bllLib.GetLibByCate("部门");
            dropDepart.DataTextField = "libValue";
            dropDepart.DataValueField = "libKey";
            dropDepart.DataBind();


            //职务下拉列表
            dropUserType.DataSource = bllLib.GetLibByCate("职务");
            dropUserType.DataTextField = "libValue";
            dropUserType.DataValueField = "libKey";
            dropUserType.DataBind();
            

            //归属城市
            ListItem item = new ListItem();
            item.Text = "--请选择--";
            item.Value = "";
            BLL.City bllcity = new BLL.City();
            dropBelongArea.DataSource = bllcity.GetList(" flag1 = '01'");
            dropBelongArea.DataTextField = "cityname";
            dropBelongArea.DataValueField = "cityname";
            dropBelongArea.DataBind();
            dropBelongArea.Items.Insert(0,item);

        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            User newUser = new User();
            string strErr = "";
            //			if(this.Dropdepart.SelectedIndex==0)
            //			{
            //				strErr+="请选择部门!";				
            //			}
            if (newUser.HasUser(txtUserName.Text))
            {
                strErr += "该用户名已存在！";
            }
            if (dropBelongArea.SelectedValue == "")
            {
                strErr += "请选择归属地";
            }

            if (strErr != "")
            {
                DataLib.Common.MessageBox.Show(this, strErr);
                return;
            }
            newUser.UserName = txtUserName.Text;
            newUser.Password = AccountsPrincipal.EncryptPassword(txtPassword.Text);
            newUser.TrueName = txtTrueName.Text;
            if (RadioButton1.Checked)
                newUser.Sex = "男";
            else
                newUser.Sex = "女";

            newUser.Phone = this.txtPhone.Text.Trim();
            newUser.Email = txtEmail.Text;
            newUser.EmployeeID = 0;
            //newUser.DepartmentID=this.Dropdepart.SelectedValue;
            newUser.Activity = true;
            newUser.UserType = dropPermiessLev.SelectedValue;
            newUser.Duty = dropUserType.SelectedValue;
            newUser.Style = 1;
            newUser.Department = dropDepart.SelectedValue; //d .SelectedValue;
            newUser.CompanyCity = dropBelongArea.SelectedValue;
            int userid = newUser.Create();
            if (userid == -100)
            {
                //this.lblMsg.Text = "该用户名已存在！";
                //this.lblMsg.Visible = true;
            }
            else
            {
                Response.Redirect("RoleAssignment.aspx?UserID=" + userid);
            }

        }
    }
}